package com.tarena.lbs.stock.web.rpc;

import com.tarena.lbs.pojo.stock.param.CouponStockParam;
import com.tarena.lbs.stock.api.StockApi;
import com.tarena.lbs.stock.web.service.StockService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@DubboService
@Slf4j
public class StockApiImpl implements StockApi {
    @Autowired
    private StockService stockService;

    /**
     * 该函数尝试初始化优惠券库存：
     * 调用stockService.saveCouponStock(param)保存优惠券库存信息。
     * 若成功，则返回true；若异常，则捕获并记录错误日志，返回false。
     * @param param 初始化优惠券股票的参数，类型为CouponStockParam
     * @return
     */
    @Override
    public boolean initCouponStock(CouponStockParam param) {
        boolean result=false;
        try{
            // 尝试保存优惠券库存信息
            result=stockService.saveCouponStock(param);
        }catch (Exception e){
            log.error("初始化优惠券:{}的库存失败",param,e);
        }
        return result;
    }


    @Override
    public boolean isEnough(Integer couponId) {
        boolean result=false;
        try{
            result=stockService.isEnough(couponId);
        }catch (Exception e){
            log.error("判断库存充足失败,优惠券id:{}",couponId,e);
        }
        return result;
    }

    @Override
    public boolean reduceStock(Integer couponId) {
        boolean result=false;
        try{
            result=stockService.reduceStock(couponId);
        }catch (Exception e){
            log.error("判断库存充足失败,优惠券id:{}",couponId,e);
        }
        return result;
    }
}
